﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.VehicleManage
{
    public partial class SaleVehicleModify : Common.PageBase
    {
        T_SALEVEHICLE_BLL bll = new T_SALEVEHICLE_BLL();
        T_VEHICLE_BLL vBll = new T_VEHICLE_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        T_CUSTOMER_BLL cBll = new T_CUSTOMER_BLL();
        private T_SUPPLIER_BLL supplierBll = new T_SUPPLIER_BLL();
        private T_MARKETDEPARTMENT_BLL marketDepartmentBll = new T_MARKETDEPARTMENT_BLL();
        private T_SERVICEPROVIDER_BLL serviceProviderBll = new T_SERVICEPROVIDER_BLL();
        private T_DEALER_BLL dealerBll = new T_DEALER_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            if (PageHelper.GetQueryString(this.Page, "OPENTYPE") == "NEWPAGE")
            {
                btnCancel.Value = "关闭";
                btnCancel.Attributes["onclick"] = "closeSelf();";
            }
            else
            {
                PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            }
            PageHelper.SetPopCalender(this.Page, txtOFFLINEDATE);
            PageHelper.SetPopCalender(this.Page, txtFIRSTSALESDATE);
            PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATE);
            PageHelper.SetPopCalender(this.Page, txtPURCHASEDATE);
            PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);
            PageHelper.SetPopCalender(this.Page, txtLASTSALEDATE);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBUSINESSDIVISIONIDBinding();

                ddlMARKETBinding();

                pageDataInit();
            }

            PageHelper.ShowMessage(this.Page, ddlMARKET.SelectedValue);
        }

        #region 页面初始化数据绑定

        private void ddlMARKETBinding()
        {
            var list = marketDepartmentBll.GetList(String.Format(" and Status = {0} ", ((int)Status.启用).ToString()));
            ddlMARKET.DataSource = list;
            ddlMARKET.DataTextField = "MarketDepartmentName";
            ddlMARKET.DataValueField = "MarketDepartmentCode";
            ddlMARKET.DataBind();
        }

        /// <summary>
        /// 供应商
        /// DDLs the SUPPLYCODE binding.
        /// </summary>
        private void ddlSUPPLYCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }

            ddlSUPPLYCODE.DataSource = supplierBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} ", businessDivisionCode, ((int)Status.启用).ToString()));
            ddlSUPPLYCODE.DataTextField = "SupplierName";
            ddlSUPPLYCODE.DataValueField = "SupplierCode";
            ddlSUPPLYCODE.DataBind();
        }

        /// <summary>
        /// 服务站
        /// DDLs the STATIONCODE binding.
        /// </summary>
        private void ddlSTATIONCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }

            ddlSTATIONCODE.DataSource = serviceProviderBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} and MarketDepartmentCode = '{2}' ", businessDivisionCode, ((int)Status.启用).ToString(), ddlMARKETDEPARTMENTCODE.SelectedValue.Trim()));
            ddlSTATIONCODE.DataTextField = "ServiceProviderName";
            ddlSTATIONCODE.DataValueField = "ServiceProviderCode";
            ddlSTATIONCODE.DataBind();
        }

        /// <summary>
        /// 经销商
        /// DDLs the DEALERCODE binding.
        /// </summary>
        private void ddlDEALERCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }

            ddlDEALERCODE.DataSource = dealerBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1}  and MarketDepartmentCode = '{2}' ", businessDivisionCode, ((int)Status.启用).ToString(), ddlMARKETDEPARTMENTCODE.SelectedValue.Trim())); ;
            ddlDEALERCODE.DataTextField = "DealerName";
            ddlDEALERCODE.DataValueField = "DealerCode";
            ddlDEALERCODE.DataBind();
        }

        /// <summary>
        /// 市场部
        /// DDLs the MARKETDEPARTMENTCODE binding.
        /// </summary>
        private void ddlMARKETDEPARTMENTCODEBinding()
        {
            string businessDivisionCode = string.Empty;
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            }
            var list = marketDepartmentBll.GetList(String.Format(" and BUSINESSDIVISIONCODE = '{0}' and Status = {1} ", businessDivisionCode, ((int)Status.启用).ToString()));
            ddlMARKETDEPARTMENTCODE.DataSource = list;
            ddlMARKETDEPARTMENTCODE.DataTextField = "MarketDepartmentName";
            ddlMARKETDEPARTMENTCODE.DataValueField = "MarketDepartmentCode";
            ddlMARKETDEPARTMENTCODE.DataBind();
        }





        /// <summary>
        ///	事业部编号 下拉列表数据绑定
        /// </summary>
        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();

            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }

        }


        #endregion

        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlMARKETDEPARTMENTCODEBinding();
            ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(null, null);
            ddlSUPPLYCODEBinding();
        }

        protected void ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERCODEBinding();
            ddlSTATIONCODEBinding();
        }

        private void pageDataInit()
        {
            string _vehicleid = String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]) ? "" : Request.QueryString["VEHICLEID"].Trim();


            if (!String.IsNullOrEmpty(_vehicleid))
            {
                T_SALEVEHICLE_Entity entity = bll.GetEntity("VEHICLEID", _vehicleid);

                ddlBUSINESSDIVISIONID.SelectedValue = entity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                ddlMARKETDEPARTMENTCODE.SelectedValue = entity.MARKETDEPARTMENTCODE;
                ddlMARKETDEPARTMENTCODE_SelectedIndexChanged(null, null);
                ddlDEALERCODEBinding();
                ddlSTATIONCODEBinding();
                ddlSUPPLYCODEBinding();
                ddlDEALERCODE.SelectedValue = entity.DEALERCODE;
                ddlSTATIONCODE.SelectedValue = entity.STATIONCODE;
                ddlSUPPLYCODE.SelectedValue = entity.SUPPLYCODE;

                PageHelper.SetControlValue<T_SALEVEHICLE_Entity>(this.Page, entity);
                txtOFFLINEDATE.Text = (entity.OFFLINEDATE == null) ? "" : entity.OFFLINEDATE.Value.ToString("yyyy-MM-dd");
                txtFIRSTSALESDATE.Text = (entity.FIRSTSALESDATE == null) ? "" : entity.FIRSTSALESDATE.Value.ToString("yyyy-MM-dd");
                txtMANUFACTUREDATE.Text = (entity.MANUFACTUREDATE == null) ? "" : entity.MANUFACTUREDATE.Value.ToString("yyyy-MM-dd");
                txtPURCHASEDATE.Text = (entity.PURCHASEDATE == null) ? "" : entity.PURCHASEDATE.Value.ToString("yyyy-MM-dd");
                txtCREATETIME.Text = (entity.CREATETIME == null) ? "" : entity.CREATETIME.Value.ToString("yyyy-MM-dd");
                txtLASTMODIFYTIME.Text = (entity.LASTMODIFYTIME == null) ? "" : entity.LASTMODIFYTIME.Value.ToString("yyyy-MM-dd");
                txtLASTSALEDATE.Text = (entity.LASTSALEDATE == null) ? "" : entity.LASTSALEDATE.Value.ToString("yyyy-MM-dd");


                lblTitle.Text = "售后车辆信息修改";
                //if (UserLoginInfo.CheckMenuRight("VehicleManage/VehicleContactList.aspx"))
                //{
                //    frameContactList.Attributes["src"] = String.Format("VehicleContactList.aspx?VINCODE={0}", entity.VINCODE.Trim());
                //}
                //if (UserLoginInfo.CheckMenuRight("ClaimFormManage/List.aspx"))
                //{
                //    frameClaimForm.Attributes["src"] = String.Format("../ClaimFormManage/List.aspx?SearchType=VehicleAssociate&VINCODE={0}", entity.VINCODE.Trim());
                //}
                hfVINCODE.Value = entity.VINCODE;
                hfENGINENUMBER.Value = entity.ENGINENUMBER;
            }
            else
            {
                string _vehicleResourcesID = PageHelper.GetQueryString(this.Page, "VehicleResourcesID");
                T_VEHICLE_Entity vEntity = vBll.GetEntity("VEHICLEID", _vehicleResourcesID);
                if (vEntity != null)
                {
                    PageHelper.SetControlValue<T_VEHICLE_Entity>(this.Page, vEntity);
                    txtOFFLINEDATE.Text = (vEntity.OFFLINEDATE == null) ? "" : vEntity.OFFLINEDATE.Value.ToString("yyyy-MM-dd");
                    txtMANUFACTUREDATE.Text = (vEntity.MANUFACTUREDATE == null) ? "" : vEntity.MANUFACTUREDATE.Value.ToString("yyyy-MM-dd");
                    txtCREATETIME.Text = "";
                    txtLASTMODIFYTIME.Text = "";
                }
                string _customerCode = PageHelper.GetQueryString(this.Page, "CustomerCode");
                T_CUSTOMER_Entity cEntity = cBll.GetEntity("CustomerCode", _customerCode);
                if (cEntity != null)
                {
                    txtCUSTOMERCODE.Text = cEntity.CUSTOMERCODE;
                    txtCUSTOMERNAME.Text = cEntity.CUSTOMERNAME;
                }
                string _tel = PageHelper.GetQueryString(this.Page, "Tel");
                if (!String.IsNullOrEmpty(_tel))
                {
                    txtCONTACTTEL.Text = _tel;
                }


                lblTitle.Text = "售后车辆信息添加";
                //phTabs.Visible = false;
                btnAddContact.Visible = false;
                btnAssociateContact.Visible = false;


                //ddlMARKETDEPARTMENTCODEBinding();
                //ddlDEALERCODEBinding();
                //ddlSTATIONCODEBinding();
                //ddlSUPPLYCODEBinding();

            }

            //if (!UserLoginInfo.IsAdministrator())
            //{
            //    ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
            //    ddlBUSINESSDIVISIONID.Enabled = false;
            //}
            txtCREATEPEOPLE.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _vehicleid = String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]) ? "" : Request.QueryString["VEHICLEID"].Trim();


            T_SALEVEHICLE_Entity entity = bll.GetEntity("VEHICLEID", _vehicleid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_SALEVEHICLE_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                try
                {

                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "salelist.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_SALEVEHICLE_Entity();

                PageHelper.GetControlValue<T_SALEVEHICLE_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "salelist.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }

            }
        }

        protected void ddlMARKET_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageHelper.ShowMessage(this.Page, ddlMARKET.SelectedValue);
        }

       

    }
}
